<?php

/**
 * This is the model class for table "pdm_perkara".
 *
 * The followings are the available columns in table 'pdm_perkara':
 * @property string $id
 * @property string $nomor_perkara
 * @property string $jns_perkara
 * @property string $wilayah
 * @property string $no_surat
 * @property string $kasus_posisi
 * @property string $surat_dakwaan
 * @property string $isi_tuntutan
 * @property integer $lama_spdp
 * @property integer $lama_tut
 * @property integer $lama_pratut
 */
class PdmPerkara extends CActiveRecord
{
	/**
	 * @return string the associated database table name
	 */
	public $terdakwa;
	public $status;
	
	public function tableName()
	{
		return 'pdm_perkara';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('id, jns_perkara, wilayah, kasus_posisi', 'required'),
			array('lama_spdp, lama_tut, lama_pratut', 'numerical', 'integerOnly'=>true),
			array('id', 'length', 'max'=>10),
			array('nomor_perkara, wilayah, no_surat, surat_dakwaan', 'length', 'max'=>100),
			array('jns_perkara', 'length', 'max'=>50),
			array('isi_tuntutan', 'safe'),
			// The following rule is used by search().
			// @todo Please remove those attributes that should not be searched.
			array('id, nomor_perkara, jns_perkara, wilayah, no_surat, kasus_posisi, surat_dakwaan, isi_tuntutan, lama_spdp, lama_tut, lama_pratut', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'id' => 'ID',
			'nomor_perkara' => 'Nomor Perkara',
			'jns_perkara' => 'Jns Perkara',
			'wilayah' => 'Wilayah',
			'no_surat' => 'No Surat',
			'kasus_posisi' => 'Kasus Posisi',
			'surat_dakwaan' => 'Surat Dakwaan',
			'isi_tuntutan' => 'Isi Tuntutan',
			'lama_spdp' => 'Lama Spdp',
			'lama_tut' => 'Lama Tut',
			'lama_pratut' => 'Lama Pratut',
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 *
	 * Typical usecase:
	 * - Initialize the model fields with values from filter form.
	 * - Execute this method to get CActiveDataProvider instance which will filter
	 * models according to data in model fields.
	 * - Pass data provider to CGridView, CListView or any similar widget.
	 *
	 * @return CActiveDataProvider the data provider that can return the models
	 * based on the search/filter conditions.
	 */
	public function search()
	{
		// @todo Please modify the following code to remove attributes that should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('id',$this->id,true);
		$criteria->compare('nomor_perkara',$this->nomor_perkara,true);
		$criteria->compare('jns_perkara',$this->jns_perkara,true);
		$criteria->compare('wilayah',$this->wilayah,true);
		$criteria->compare('no_surat',$this->no_surat,true);
		$criteria->compare('kasus_posisi',$this->kasus_posisi,true);
		$criteria->compare('surat_dakwaan',$this->surat_dakwaan,true);
		$criteria->compare('isi_tuntutan',$this->isi_tuntutan,true);
		$criteria->compare('lama_spdp',$this->lama_spdp);
		$criteria->compare('lama_tut',$this->lama_tut);
		$criteria->compare('lama_pratut',$this->lama_pratut);

		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
		));
	}
	
	
	 public function searchIndex()
	{
		$criteria = new CDbCriteria;
		
		$criteria->alias = 'a';
		$criteria->select = "a.id,a.nomor_perkara, a.wilayah, b.terdakwa, b.status"; //, b.terdakwa, b.status";
		$criteria->join = "left join
						   (select id_perkara, group_concat(nama SEPARATOR '<br/>') as terdakwa, group_concat(status SEPARATOR '<br/>') as status
						   FROM pdm_tersangka group by id_perkara) b  
						    on a.id = b.id_perkara";
		/*
		if($terdakwa != null){
			$criteria->condition = "substring(b.npwpd from 3 for 7) = '$no_pokok'";
		}else
		if($no_pokok != null and $no_data != null and $tahun == null and $no_rekening == '--Pilih Rekening--'){
			$criteria->condition = "substring(b.npwpd from 3 for 7) = '$no_pokok' and a.no_data = '$no_data'";
		} 
		*/
		 $criteria->order = 'a.id DESC';
		
		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
		));
	} 
	
		 public function searchDetil($id)
	{
		$criteria = new CDbCriteria;
		
		$criteria->alias = 'a';
		$criteria->select = "a.id,a.nomor_perkara, a.wilayah, b.terdakwa, b.status"; //, b.terdakwa, b.status";
		$criteria->join = "left join
						   (select id_perkara, group_concat(nama SEPARATOR '<br/>') as terdakwa, group_concat(status SEPARATOR '<br/>') as status
						   FROM pdm_tersangka group by id_perkara) b  
						    on a.id = b.id_perkara";
		$criteria->condition = "a.id = '$id'";
		
		
		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
		));
	} 

	/**
	 * Returns the static model of the specified AR class.
	 * Please note that you should have this exact method in all your CActiveRecord descendants!
	 * @param string $className active record class name.
	 * @return PdmPerkara the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}
}
